package com.gxkj.drm.uitls;

import org.springframework.util.StringUtils;

/**
 * @author shaoxiangfei
 */
public class DrmEnv {

    private static final String ENVIRONMENT = "DRM_ENV";

    private static final String DRM_HOST_NAME = "java.rmi.server.hostname";

    private static final String DRM_AUTH_DIR = "rmi.auth.dir";

    private static final String DRM_PORT = "rmi.port";

    /**
     *
     * 获取当前服务的端口号
     *
     * @return
     */
    public static String getDrmPort() {
        // 优先 -Ddrm.port=port
        String drmPort = System.getProperty(DRM_PORT);
        if (StringUtils.isEmpty(drmPort)) {
            return null;
        }

        return drmPort;
    }

    /**
     *
     * 获取当前服务drm认证文件目录
     *
     * @return
     */
    public static String getDrmAuthDir() {
        // 优先 -Ddrm.auth.dir=/dir
        String authDir = System.getProperty(DRM_AUTH_DIR);
        if (StringUtils.isEmpty(authDir)) {
            return null;
        }

        return authDir;
    }

    /**
     *
     * 获取当前服务ip
     *
     * @return
     */
    public static String getDrmHostName() {
        // 优先 -Djava.rmi.server.hostname=host
        String ip = System.getProperty(DRM_HOST_NAME);
        if (StringUtils.isEmpty(ip)) {
            return   IpUtil.getLocalIP();
        }

        return ip;
    }

    /**
     * 获取当前环境
     *
     * @return
     */
    public static String getEnvironment() {
        // 优先 -DMISS_ENV=test
        String environment = System.getProperty(ENVIRONMENT);
        // export MISS_ENV=prod
        if (StringUtils.isEmpty(environment)) {
            environment = System.getenv(ENVIRONMENT);
        }

        if (StringUtils.isEmpty(environment)) {
            environment = ENV_TYPE.DEV.getEnv();
        }

        return environment.toLowerCase();
    }

    public enum ENV_TYPE {

        DEV("dev"), //开发环境

        TEST("test"), //测试环境

        PROD("prod"); //生产环境

        private String env;

        ENV_TYPE(String env) {
            this.env = env;
        }

        public String getEnv() {
            return this.env;
        }
    }

    /**
     * 是否大陆测试环境
     *
     * @return
     */
    public static final boolean isTest() {
        return getEnvironment().equals(ENV_TYPE.TEST.getEnv());
    }
}
